package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * desc
 *
 * @Author itcast
 * @Create 2023/4/29
 **/
@Mapper
public interface OrderMapper {

    /**
     * 插入订单数据
     * @param orders
     */
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 用户端查询订单详情
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    OrderVO getByIdVO(Long id);



    /**
     * 订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> adminPageQuery(OrdersPageQueryDTO ordersPageQueryDTO);


    /**
     * 各个状态的订单数量统计
     * @return
     */
    @Select("select count(*) from orders where status =#{3}")
    Integer searchStatis3();
    @Select("select count(*) from orders where status =#{4}")
    Integer searchStatis4();
    @Select("select count(*) from orders where status =#{2}")
    Integer searchStatis2();

    /**
     * 查询订单详情
     * @return
     */
    @Select("select * from orders where id =#{id}")
    OrderVO adminGetById(Long id);

    /**
     * 接单
     * @param id
     */
    @Update("update orders set status = 3 where id = #{id}")
    void updateConfirm(Long id);

    /**
     * 拒单
     * @param rejectionReason
     * @param id
     */
    @Update("update orders set  status = 6 ,rejection_reason = #{rejectionReason} where id =#{id}")
    void updateReject(String rejectionReason, Long id);


    /**
     *取消订单＋如果支付则退款
     * @param id
     * @param cancelReason
     */
    @Update("update orders set status = 6 ,cancel_reason = #{cancelReason} where id =#{id}")
    void updateCancel(Long id, String cancelReason);

    @Select("select * from orders where id = #{id}")
    Orders getById(Long id);


    /**
     * 派送订单
     * @param id
     */
    @Update("update orders set status = 4 where id =#{id}")
    void updateDelivery(Integer id);

    /**
     * 完成订单
     * @param id
     */
    @Update("update orders set status = 5 where id =#{id}")
    void updateComplete(Integer id);
    /**
     * 根据订单状态和下单时间查询订单
     * @param status
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getByStatusAndOrderTimeLT(Integer status, LocalDateTime time);


    /**
     * 营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);

    /**
     * 统计指定时间区间内的销量排名数据
     * @param beginTime
     * @param endTime
     * @return
     */
    List<GoodsSalesDTO> getTop10(LocalDateTime beginTime, LocalDateTime endTime);
}
